﻿var ServiceHelper = (function (doc) {
    var servicePath = ConstantHelper.Page.ServiceAddress, functionName, taskGuid, userID, dataGuid, dataType, result, xmlParam, dtype;

    function login() {
        $servicePath = getservicepath(ConstantHelper.Page.TransformData);
        $.ajax({
            type: "POST",
            url: $servicePath,
            data: { UserID: userID, taskGuid: taskGuid, XmlTransform: xmlParam },
            async: false,
            success: function (request, textStatus) {
                result = request.text;
            },
            error: function (request, textStatus) {
                result = "-1";
            }
        });
        return result;
    }

    function initparameters(sp, ud, tg, dg, dt, xp) {
        servicePath = (sp == null ? servicePath : sp);
        taskGuid = (tg == null ? taskGuid : tg);
        userID = (ud == null ? userID : ud);
        dataGuid = (dg == null ? dataGuid : dg);
        dataType = (dt == null ? dataType : dt);
        xmlParam = (xp == null ? xmlParam : xp);
        result = null;
    }

    function getcurrenttime() {
        initparameters(null, ConstantHelper.Page.UserID, ConstantHelper.Page.CurrentTimeTaskGuid, ConstantHelper.Page.CurrentTimeDataGuid, ConstantHelper.Page.CurrentTime);
        result = getdata();
        return WebHelper.fun.getXmlforJQ(result).find("Time").eq(0).text();
    }

    function getnewguid() {
        initparameters(null, "", "", "", ConstantHelper.Page.NewGuid);
        result = getdata();
        return result;
    }

    function getdata() {
        $servicePath = getservicepath(ConstantHelper.Page.GetData);
        $.ajax({
            type: "POST",
            url: $servicePath,
            data: { UserID: userID, TaskGuid: taskGuid, DataGuid: dataGuid, DataType: dataType },
            async: false,
            success: function (request, textStatus) {
                result = request.text;
            },
            error: function (request, textStatus) {
                result = "-1";
            }
        });
        return result;
    }

    function setdata() {
        $servicePath = getservicepath(ConstantHelper.Page.SetData);
        $.ajax({
            type: "POST",
            url: $servicePath,
            data: { UserID: userID, TaskGuid: taskGuid, DataGuid: dataGuid, DataType: dataType, xmlData: xmlParam },
            async: false,
            success: function (request, textStatus) {
                result = request.text;
            },
            error: function (request, textStatus) {
                result = "-1";
            }
        });
        return result;
    };

    function transformdata() {
        $servicePath = getservicepath(ConstantHelper.Page.TransformData);
        $.ajax({
            type: "POST",
            url: $servicePath,
            data: { UserID: userID, taskGuid: taskGuid, XmlTransform: xmlParam },
            async: false,
            success: function (request, textStatus) {
                result = request.text;
            },
            error: function (request, textStatus) {
                result = "-1";
            }
        });
        return result;
    };

    function transformdataforgis() {
        $servicePath = getservicepath("GisTransformCustomData");
        $.ajax({
            type: "POST",
            url: $servicePath,
            data: { UserID: userID, taskGuid: taskGuid, XmlTransform: xmlParam },
            async: false,
            success: function (request, textStatus) {
                result = request.text;
            },
            error: function (request, textStatus) {
                result = "-1";
            }
        });
        return result;
    };

    function transformdata2(_b, _s, _e, _u, _t, _x) {
        $servicePath = getservicepath(ConstantHelper.Page.TransformData);
        $.ajax({
            type: "POST",
            url: $servicePath,
            data: { UserID: _u, taskGuid: _t, XmlTransform: _x },
            async: true,
            beforeSend: _b,
            success: _s,
            error: _e
        });
    };

    function getdata2(_b, _s, _e, _u, _t, _g, _dt) {
        $servicePath = getservicepath(ConstantHelper.Page.GetData);
        $.ajax({
            type: "POST",
            url: $servicePath,
            data: { UserID: _u, TaskGuid: _t, DataGuid: _g, DataType: _dt },
            async: true,
            beforeSend: _b,
            success: _s,
            error: _e
        });
        return result;
    }

    function getservicepath(fn) {
        $servicePath = servicePath + "/" + fn;
        return $servicePath;
    }

    return {
        Login: function () { return login(); },
        GetData: function () { return getdata(); },
        SetData: function () { return setdata(); },
        TransformData: function () { return transformdata(); },
        TransformDataforGis: function () { return transformdataforgis(); },
        TransformData2: function (_b, _s, _e, _u, _t, _x) { return transformdata2(_b, _s, _e, _u, _t, _x); },
        GetData2: function (_b, _s, _e, _u, _t, _g, _dt) { return getdata2(_b, _s, _e, _u, _t, _g, _dt); },
        InitParameters: function (sp, ud, tg, dg, dt, xp) {
            initparameters(sp, ud, tg, dg, dt, xp);
        },
        GetCurrentTime: function () { return getcurrenttime(); },
        GetNewGuid: function () { return getnewguid(); }
    }
})(this.document);